package ar.Location.Api.GooglePlace;

import java.io.InputStream;

import javax.xml.parsers.SAXParserFactory;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import ar.Location.Constant.Define;
import ar.Location.Helper.BitmapProvider;
import ar.Location.Helper.CalculationProvider;
import ar.Location.Helper.HttpProvider;
import ar.Location.Helper.StringHelper;
import ar.Location.PlaceObject.Place;
import ar.Location.UI.PlaceResultItemView;
import ar.activity.PlaceDetailActivity;
import ar.activity.R;
import ar.activity.R.drawable;
import ar.globals.Globals;

public class GooglePlace extends Place {

	private static final String TAG = "GooglePlace";
	public boolean isUpdateDetailInfo = false;

	public GooglePlace(String dfName) {
		if (dfName.equals("cafe")) {
			categoryName = Define.DF_CAFE;
		} else if (dfName.equals("food")) {
			categoryName = Define.DF_FOOD;
		} else if (dfName.equals("park")) {
			categoryName = Define.DF_PARK;
		} else if (dfName.equals("bank")) {
			categoryName = Define.DF_BANK;
		} else if (dfName.equals("university")) {
			categoryName = Define.DF_UNIVERSITY;
		} else if (dfName.equals("hospital")) {
			categoryName = Define.DF_HOSPITAL;
		} else {
			categoryName = Define.DS_GOOGLE_PLACE;
		}

		websiteUrl = "https://maps.google.com/maps";
	}

	@Override
	public void bindDataToViewDetail(final PlaceDetailActivity detailView) {
		// TODO Auto-generated method stub
		if (detailView != null) {
			if (!isUpdateDetailInfo) {
				final String reference = other;
				if (reference != null && reference.length() > 0) {
					Thread threadUpdateDetailInfo = new Thread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							updateDetailInfo(reference);
							Message msg = new Message();
							Bundle data = new Bundle();
							data.putString("phone", phoneNumber);
							data.putString("url", websiteUrl);
							msg.setData(data);
							if (detailView.handlerUpdateInfo != null) {
								detailView.handlerUpdateInfo.sendMessage(msg);
							}
						}
					});
					threadUpdateDetailInfo.start();
				}
			}

			detailView.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if (categoryName == Define.DF_CAFE) {
						detailView.imgViewIcon
								.setImageBitmap(BitmapProvider.bmpIconCafe);
					} else if (categoryName == Define.DF_FOOD) {
						detailView.imgViewIcon
								.setImageBitmap(BitmapProvider.bmpIconFood);
					} else if (categoryName.equals(Define.DF_PARK)) {
						detailView.imgViewIcon
								.setImageBitmap(BitmapProvider.bmpIconPark);
					} else if (categoryName.equals(Define.DF_BANK)) {
						detailView.imgViewIcon
								.setImageBitmap(BitmapProvider.bmpIconBank);
					} else if (categoryName.equals(Define.DF_UNIVERSITY)) {
						detailView.imgViewIcon
								.setImageBitmap(BitmapProvider.bmpIconUniversity);
					} else if (categoryName.equals(Define.DF_HOSPITAL)) {
						detailView.imgViewIcon
								.setImageBitmap(BitmapProvider.bmpIconHospital);
					} else {
						detailView.imgViewIcon
								.setImageResource(drawable.icon_google_place);
					}

					detailView.txtViewName.setText(name);
					detailView.txtViewDescription.setText(address);

					if (imageUrl == null || imageUrl.length() == 0) {
						detailView.imgViewThumb
								.setImageResource(drawable.icon_google_place);
						detailView.imgViewThumb.setVisibility(View.VISIBLE);
						detailView.progressBarUpdateThumbImage
								.setVisibility(View.GONE);
					}

					if (latitude.length() > 0 && longitude.length() > 0) {
						float myLat = Float.parseFloat(Globals.LAT);
						float myLng = Float.parseFloat(Globals.LNG);
						float lat = Float.parseFloat(latitude);
						float lng = Float.parseFloat(longitude);
						float dist = CalculationProvider.distFrom(myLat, myLng,
								lat, lng);
						String strDistance = StringHelper
								.getStringDistance(Math.round(dist));
						detailView.txtViewDistance.setText(String
								.valueOf(strDistance));
					} else {
						detailView.txtViewDistance.setText("");
					}

					detailView.txtViewAddress.setVisibility(View.GONE);
					detailView.imgViewBulletAddress.setVisibility(View.GONE);

					if (websiteUrl != null && websiteUrl.length() > 0) {
						detailView.txtViewWebsiteUrl.setText("Website: "
								+ websiteUrl);
					} else {
						detailView.txtViewWebsiteUrl.setVisibility(View.GONE);
						detailView.imgViewBulletWebsite
								.setVisibility(View.GONE);
						detailView.actionVisitWebsite.setVisibility(View.GONE);
					}

					if (phoneNumber != null && phoneNumber.length() > 0) {
						detailView.txtViewPhoneNumber.setText("Phone number: "
								+ phoneNumber);
					} else {
						detailView.txtViewPhoneNumber.setVisibility(View.GONE);
						detailView.imgViewBulletPhoneNumber
								.setVisibility(View.GONE);
						detailView.actionCall.setVisibility(View.GONE);
					}

					detailView.txtViewDateTime.setVisibility(View.GONE);
					detailView.imgViewBulletDateTime.setVisibility(View.GONE);
				}
			});
		}
	}

	@Override
	public void bindDataToListViewItem(final PlaceResultItemView listViewItem) {
		// TODO Auto-generated method stub
		if (listViewItem != null) {
			((Activity) listViewItem.listViewItemContext)
					.runOnUiThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							if (categoryName.equals(Define.DF_CAFE)) {
								listViewItem.imgIconName
										.setImageBitmap(BitmapProvider.bmpIconCafe);
							} else if (categoryName.equals(Define.DF_FOOD)) {
								listViewItem.imgIconName
										.setImageBitmap(BitmapProvider.bmpIconFood);
							} else if (categoryName.equals(Define.DF_PARK)) {
								listViewItem.imgIconName
										.setImageBitmap(BitmapProvider.bmpIconPark);
							} else if (categoryName.equals(Define.DF_BANK)) {
								listViewItem.imgIconName
										.setImageBitmap(BitmapProvider.bmpIconBank);
							} else if (categoryName
									.equals(Define.DF_UNIVERSITY)) {
								listViewItem.imgIconName
										.setImageBitmap(BitmapProvider.bmpIconUniversity);
							} else if (categoryName.equals(Define.DF_HOSPITAL)) {
								listViewItem.imgIconName
										.setImageBitmap(BitmapProvider.bmpIconHospital);
							} else {
								listViewItem.imgIconName
										.setImageResource(R.drawable.icon_google_place);
							}
							listViewItem.imgIconDescription
									.setImageResource(R.drawable.icon_bullet);
							listViewItem.txtViewName.setText(name);
							if (address != null && address.length() > 0) {
								listViewItem.txtViewDescription
										.setText(address);
							}

							if (latitude.length() > 0 && longitude.length() > 0) {
								float myLat = Float.parseFloat(Globals.LAT);
								float myLng = Float.parseFloat(Globals.LNG);
								float lat = Float.parseFloat(latitude);
								float lng = Float.parseFloat(longitude);
								float dist = CalculationProvider.distFrom(
										myLat, myLng, lat, lng);
								String strDistance = StringHelper
										.getStringDistance(Math.round(dist));
								listViewItem.txtViewDistance.setText(String
										.valueOf(strDistance));
							} else {
								listViewItem.txtViewDistance.setText("");
							}
						}
					});
		}
	}

	public void updateImage(final PlaceResultItemView listViewItem,
			final Bitmap bmp) {
		// TODO Auto-generated method stub
		if (listViewItem != null) {
			((Activity) listViewItem.listViewItemContext)
					.runOnUiThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							listViewItem.imgView.setVisibility(View.VISIBLE);
							listViewItem.progressBar.setVisibility(View.GONE);
							if (bmp != null) {
								listViewItem.imgView.setImageBitmap(bmp);
							} else {
								listViewItem.imgView.setImageBitmap(null);
							}
						}
					});
		}

	}

	private void updateDetailInfo(String reference) {
		// TODO Auto-generated method stub
		try {
			if (!isUpdateDetailInfo) {
				String url = Define.GOOGLE_API_PLACE_DETAIL_REQUEST;
				url = url.replace("@key", Define.GOOGLE_API_KEY);
				url = url.replace("@reference", reference);
				url = url.replace("@sensor", "true");

				SAXParserFactory spf = SAXParserFactory.newInstance();
				spf.setValidating(false);
				InputStream inputStream = HttpProvider.getInputStream(url);
				GooglePlaceDetailHandler googlePlaceDetailHandler = new GooglePlaceDetailHandler(
						categoryName);
				Xml.parse(inputStream, Xml.Encoding.UTF_8,
						googlePlaceDetailHandler);
				Place p = googlePlaceDetailHandler.getPlaceDetail();

				if (p != null) {
					rank = p.rank;
					address = p.address;
					websiteUrl = p.websiteUrl;
					phoneNumber = p.phoneNumber;
				}

				isUpdateDetailInfo = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.d(TAG, e.getMessage());
		}
	}

}
